import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.transfer_table模块已被废弃，替代模块为qturboframe.public_ui.h_ui.transfer_table", DeprecationWarning)

from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from qturboframe.lib.ranorex.web_element.button_tag import ButtonTag
from ..ucf_base_element import BaseElement
from .edit_grid import EditGrid


class TransferTable(BaseElement):

    _left_table = EditGrid
    _right_table = EditGrid

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        self._element = DivTag(self._element.control)
        self.left_element = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-transfer-table-list\'][1]', timeout=self.timeout).control)
        self.right_element = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-transfer-table-list\'][-1]', timeout=self.timeout).control)
        self.operate_element = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-transfer-table-operation\']', timeout=self.timeout).control)
        self.left_table()
        self.right_table()

    def left_table(self, idname=None, father=None):
        '''
        获取左边表格对象

        :Args:
         - idname: 左边表格的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 左边表格的祖先级元素，可以给左边表格查找限定范围，这样就不需要在整个页面中查找

        :Example:
         - 例: left_table = transfer_table.left_table(By.CSS_SELECTOR, 'div.h-editgird-wrapper')
        '''
        if father is None:
            father = self.left_element
        if idname:
            self._left_table = EditGrid(idname, father, self.timeout)
        else:
            self._left_table = EditGrid(
                {'xpath':
                     './/ul[@class~\'h-transfer-table-list-content\']/div[@class~\'h-editgird-wrapper\']'},
                father, self.timeout)
        return self._left_table

    def right_table(self, idname=None, father=None):
        '''
        获取右边表格对象

        :Args:
         - idname: 右边表格的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 右边表格的祖先级元素，可以给右边表格查找限定范围，这样就不需要在整个页面中查找

        :Example:
         - 例: right_table = transfer_table.right_table(By.CSS_SELECTOR, 'div.h-editgird-wrapper')
        '''
        if father is None:
            father = self.right_element
        if idname:
            self._right_table = EditGrid(idname, father, self.timeout)
        else:
            self._right_table = EditGrid(
                {'xpath':
                     './/ul[@class~\'h-transfer-table-list-content\']/div[@class~\'h-editgird-wrapper\']'},
                father, self.timeout)
        return self._right_table

    def select_left_table(self, table_dict):
        '''
        根据单元格列名和单元格内容勾选左边表格中所在的行

        :Args:
         - table_dict: 单元格列名和单元格内容，字典类型，例：{列名1:内容1,列名2:内容2}

        :Example:
         - 例: transfer_table.select_left_table({'姓名':'admin, manager'})
        '''
        self._left_table.select_rows(table_dict)

    def select_right_table(self, table_dict):
        '''
        根据单元格列名和单元格内容勾选右边表格中所在的行

        :Args:
         - table_dict: 单元格列名和单元格内容，字典类型，例：{列名1:内容1,列名2:内容2}

        :Example:
         - 例: transfer_table.select_right_table({'姓名':'admin, manager'})
        '''
        self._right_table.select_rows(table_dict)

    def select_left_table_all(self):
        '''
        左边表格行全选

        :Example:
         - 例: transfer_table.select_left_table_all()
        '''
        self._left_table.select_all()

    def select_right_table_all(self):
        '''
        右边表格行全选

        :Example:
         - 例: transfer_table.select_left_table_all()
        '''
        self._right_table.select_all()

    def to_right(self):
        '''
        转移到右边表格

        :Example:
         - 例: transfer_table.to_right()
        '''
        to_right_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
            'xpath', './button[@class~\'h-btn-primary\']', timeout=self.timeout)[1].control)
        to_right_btn.secure_click()

    def to_left(self):
        '''
        转移到左边表格

        :Example:
         - 例: transfer_table.to_left()
        '''
        to_left_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
            'xpath', './button[@class~\'h-btn-primary\']', timeout=self.timeout)[0].control)
        to_left_btn.secure_click()

    def all_to_right(self):
        '''
        全部转移到右边表格

        :Example:
         - 例: transfer_table.all_to_right()
        '''
        all_to_right_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
            'xpath', './button[@class~\'h-btn-ghost\']', timeout=self.timeout)[1].control)
        all_to_right_btn.secure_click()

    def all_to_left(self):
        '''
        全部转移到左边表格

        :Example:
         - 例: transfer_table.all_to_left()
        '''
        all_to_left_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
            'xpath', './button[@class~\'h-btn-ghost\']', timeout=self.timeout)[0].control)
        all_to_left_btn.secure_click()
